using System.Web;
using System.Web.Security;
using Petshop.Backend.Logic;
using Petshop.Backend.Data;

namespace PetShop.Web.ProcessFlow
{
	
	public class AccountController {
		

		private const string ACCOUNT_KEY = "ACCOUNT_KEY";
		private const string URL_DEFAULT = "default.aspx";
		private const string URL_SIGNIN  = "SignIn.aspx";
		private const string URL_ACCOUNTCREATE = "MyAccount.aspx?action=create";
		private const string URL_ACCOUNTSIGNIN = "MyAccount.aspx?action=signIn";
		private const string URL_ACCOUNTUPDATE = "MyAccount.aspx?action=update";
		
		
		public AccountController(){
		}

		public bool ProcessLogin(string userId, string password){

			// Use the account business logic layer to login
			var accountSvc = new AccountService();
			Account myAccountInfo = accountSvc.SignIn(userId, password);

			//If login is successful then store the state in session and redirect
			if (myAccountInfo != null) {
				HttpContext.Current.Session[ACCOUNT_KEY] = myAccountInfo;
				
				// Determine where to redirect the user back too
				// If they came in from the home page, take them to a similar page
				if (FormsAuthentication.GetRedirectUrl(userId, false).EndsWith(URL_DEFAULT)) {

					FormsAuthentication.SetAuthCookie(userId, false);
					HttpContext.Current.Response.Redirect(URL_ACCOUNTSIGNIN, true);

				}else{
					// Take the customer back to where the came from
					FormsAuthentication.SetAuthCookie(userId, false);

					HttpContext.Current.Response.Redirect(FormsAuthentication.GetRedirectUrl(userId, false), true);
				}

				return true;
			
			}else {
				// Login has failed so return false
				return false;
			}
		}

		public bool CreateAccount(Account newAccountInfo){

			try {
				// Creata a new business logic tier
                var account = new AccountService();

				// Call the insert method
				account.CreateNewAccount(newAccountInfo);

				// Store the data in session state and store the authenticated cookie
				HttpContext.Current.Session[ACCOUNT_KEY] = newAccountInfo;
				FormsAuthentication.SetAuthCookie(newAccountInfo.UserId, false);
				
				//Finally forward to the welcome page
				HttpContext.Current.Response.Redirect(URL_ACCOUNTCREATE, true);
				
			
			}catch {
				return false;
			}

			return true;
		}
	
		
		public void UpdateAccount(Account updatedAccountInfo){

			// Create the business logic tier
            var account = new AccountService();
			
			// Call the udpate method
			account.UpdateAccount(updatedAccountInfo);

			//Store the update info back in session state
			HttpContext.Current.Session[ACCOUNT_KEY] = updatedAccountInfo;

			//Redirect the user to the my account page
			HttpContext.Current.Response.Redirect(URL_ACCOUNTUPDATE, true);
			
		}

      


		public Account GetAccountInfo(bool required){
			Account myAccount = (Account)HttpContext.Current.Session[ACCOUNT_KEY];

			if (myAccount == null){
				if(required){
					HttpContext.Current.Response.Redirect(URL_SIGNIN, true);
					
				}
				return null;
			}else{
				return myAccount;
			}
		}

        public string GetBannerPath(string categoryKey)
        {
            if (string.IsNullOrEmpty(categoryKey)) return null;
           
            var fcd = new CatalogService();

            var cat = fcd.GetCategoryById(categoryKey);

            string profileGetBannerPath = cat.BannerData;

            return profileGetBannerPath;

        }

		public string GetFavouriteCategory(){

			Account myAccount = (Account)HttpContext.Current.Session[ACCOUNT_KEY];

			if (myAccount != null && !string.IsNullOrEmpty(myAccount.FavCategory)) {
                return myAccount.FavCategory;
			}else{
				return null;
			}
		}
	
		
		public void LogOut(){

			
			FormsAuthentication.SignOut();
			
			HttpContext.Current.Session.Clear();
			
			
			HttpContext.Current.Session.Abandon();
		}
	}
}
